<?php

 include("../include/config.php");
 include("../include/functions/import.php");
 require_once($config['basedir'] . "/ajax/cpaint2.inc.php"); 

  $cp = new cpaint();
  $cp->register('member_subscribe');
  $cp->register('check_username');
  $cp->register('profile_comments');
  $cp->register('add_to_friends');
  $cp->register('send_message');
  $cp->register('fav_audio');
  $cp->register('rate_audio');
  $cp->register('audio_comments');
  $cp->register('audio_report');
  $cp->register('audio_share');
  $cp->start();
  $cp->return_data(); 

// Subscribe To Member Begin
function member_subscribe($subscribee,$subscriber)
{
	 global $cp; 
 	 global $conn;
	 
	 if ($subscribee < 1)
	 {
	 	$submessage="0";
	 }
	 else
	 {
	 	$query="insert into members_subscribers set subscriber='".mysql_real_escape_string($subscriber)."', subscribee='".mysql_real_escape_string($subscribee)."'";
	 	$conn->execute($query);
	 	if(mysql_affected_rows()==1)
	 	{ 
			$submessage="1";
	 	}
	 	else
	 	{ 
			$submessage="0";
	 	}
	}
	 $a_result_node =& $cp->add_node("submessage");
 	 $a_result_node->set_data($submessage);
}
// Subscribe To Member End

// Check Username Begin
function check_username($cusernamed)
{
	 global $cp; 
 	 global $conn;
	 	 
	 if ($cusernamed == "")
	 {
	 	$cunamemsg="2";
	 }
	 else
	 {
	 	$query="SELECT USERID FROM members where username='".mysql_real_escape_string($cusernamed)."'";
	 	$conn->execute($query);
	 	if(mysql_affected_rows()>=1)
	 	{ 
			$cunamemsg="0";
	 	}
	 	else
	 	{ 
			$cunamemsg="1";
	 	}
	 }
	 $a_result_node =& $cp->add_node("cunamemsg");
 	 $a_result_node->set_data($cunamemsg);
}
// Check Username End

// Audio Comment Begin
function audio_comments($details,$userid,$audioid)
{
	global $cp, $conn; 
    $details=htmlentities(strip_tags($details),ENT_QUOTES,'UTF-8'); 

	$query="insert into audios_comments set AUDIOID='".mysql_real_escape_string($audioid)."', USERID='".mysql_real_escape_string($userid)."', details='".mysql_real_escape_string($details)."', time_added='".time()."'";
	$conn->execute($query);
	
	if(mysql_affected_rows()==1)
	{
		$query="update audios set commentcount=commentcount+1 WHERE AUDIOID='".mysql_real_escape_string($audioid)."'";
		$conn->execute($query);
	}
	if(mysql_affected_rows()>=1)
	{
		$acommsg = 1;
	}
	else
	{ 
		$acommsg = 0;
	}
		
	$a_result_node =& $cp->add_node("acommsg");
 	$a_result_node->set_data($acommsg);
}
// Audio Comment End

// Audio Rating Begin
function rate_audio($therating,$theaudioid)
{
	global $cp, $conn; 
	
	if ((is_numeric($therating)) && ($therating >= 2) && ($therating <= 10))
	{			
		$alreadyvoted = ajax_rate_audio($theaudioid,$therating);

		if($alreadyvoted)
		{
			$a_result_node =& $cp->add_node("astatus");
			$a_result_node->set_data('1');
		}
		else
		{
			$a_result_node =& $cp->add_node("astatus");
			$a_result_node->set_data('0');
		}
	
	}
	else
	{
		$a_result_node =& $cp->add_node("astatus");
		$a_result_node->set_data('2');
	}
}
// Audio Rating End

// Favorite Audio Begin
function fav_audio($fuserid,$faudioid)
{
	 global $cp; 
 	 global $conn;
	 
	 if ($fuserid < 1)
	 {
	 	$favaudmessage="0";
	 }
	 else
	 {
	 	$query="insert into audios_favorited set USERID='".mysql_real_escape_string($fuserid)."', AUDIOID='".mysql_real_escape_string($faudioid)."'";
	 	$conn->execute($query);
	 	if(mysql_affected_rows()==1)
	 	{ 
			$favaudmessage="1";
	 	}
	 	else
	 	{ 
			$favaudmessage="0";
	 	}
	}
	 $a_result_node =& $cp->add_node("favaudmessage");
 	 $a_result_node->set_data($favaudmessage);
}
// Favorite Audio End

// Share Audio Begin
function audio_share($asharemessage,$ashareto,$userid,$audioid)
{
	global $cp, $conn, $config; 
    $asharemessage=htmlentities(strip_tags($asharemessage),ENT_QUOTES,'UTF-8');
	$query="SELECT title FROM audios WHERE AUDIOID='".mysql_real_escape_string($audioid)."'";
	$executequery=$conn->execute($query);
	$temptitle = $executequery->fields[title];
	$tempseotitle = seo_clean_titles_sendmail($temptitle);
	$shareaudiolink = $config['baseurl']."/audios/".$audioid."/".$tempseotitle;
	$ashareemails = explode(",",$ashareto);
	$asentemailscount = 0;
	$asentmailfailed = 0;
	for($asendloop=0; $asendloop<count($ashareemails); $asendloop++)
	{
			$sendtotemp=$ashareemails[$asendloop];
			
			if(verify_valid_email($sendtotemp))
			{
				
				$query="SELECT email,username FROM members WHERE USERID='".mysql_real_escape_string($userid)."'";
				$executequery=$conn->execute($query);
				$tempemail = $executequery->fields[email];
				$tempusername = $executequery->fields[username];
				
				$sendername = stripslashes(stripslashes($config['site_name']));
				
				$from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='shareaudio'";
            	$executequery = $conn->execute($query);
				$subject = $tempemail." ".$executequery->fields['subject'];
				
				$sendmailbody = "<html>";
				$sendmailbody .= $tempusername." thought you would enjoy this audio:<br>";
				$sendmailbody .= "<a href=$shareaudiolink>$shareaudiolink</a><br><br>";
				if ($asharemessage != "")
				{
					$asharemessage = str_replace("\\n", "<br>", $asharemessage);
					$sendmailbody .= $tempusername." also included this message for you:<br>";
					$sendmailbody .= $asharemessage."<br><br>";
				}
				$sendmailbody .= "If you cannot click the link, then visit this url:<br>";
				$sendmailbody .= $shareaudiolink."<br><br>";
				$sendmailbody .= "Enjoy The Audio,<br>";
				$sendmailbody .= "$sendername";
				$sendmailbody .= "</html>";
				mailme($sendtotemp,$sendername,$from,$subject,$sendmailbody,$bcc="");
				$asentemailscount++;
			}
			else
			{
				$asentmailfailed = 1;
			}
	}
	
	if($asentemailscount>=1)
	{
		if($asentmailfailed == "1")
		{
			$ashrmsg = 2;
		}
		else
		{
			$ashrmsg = 1;
		}
	}
	else
	{ 
		$ashrmsg = 0;
	}
		
	$a_result_node =& $cp->add_node("ashrmsg");
 	$a_result_node->set_data($ashrmsg);
}
// Share Audio End

// Report Audio Begin
function audio_report($arepremarks,$arepreason,$userid,$audioid)
{
	global $cp, $conn; 
    $arremarks=htmlentities(strip_tags($arepremarks),ENT_QUOTES,'UTF-8');
	$arreason=htmlentities(strip_tags($arepreason),ENT_QUOTES,'UTF-8');

	$query="insert into audios_reports set AUDIOID='".mysql_real_escape_string($audioid)."', USERID='".mysql_real_escape_string($userid)."', reason='".mysql_real_escape_string($arreason)."', remarks='".mysql_real_escape_string($arremarks)."', time='".time()."', date='".date("Y-m-d")."'";
	$conn->execute($query);
	
	if(mysql_affected_rows()>=1)
	{
		$arepmsg = 1;
	}
	else
	{ 
		$arepmsg = 0;
	}
		
	$a_result_node =& $cp->add_node("arepmsg");
 	$a_result_node->set_data($arepmsg);
}
// Report Audio End

// Profile Comment Begin
function profile_comments($details,$userid,$profileid)
{
	global $cp, $conn; 
    $details=htmlentities(strip_tags($details),ENT_QUOTES,'UTF-8'); 

	$query="insert into members_comments set PROFILEID='".mysql_real_escape_string($profileid)."', USERID='".mysql_real_escape_string($userid)."', details='".mysql_real_escape_string($details)."', time_added='".time()."'";
	$conn->execute($query);
	
	if(mysql_affected_rows()>=1)
	{
		$pcommsg = 1;
	}
	else
	{ 
		$pcommsg = 0;
	}
		
	$a_result_node =& $cp->add_node("pcommsg");
 	$a_result_node->set_data($pcommsg);
}
// Profile Comment End

// Send Message Begin
function send_message($sendmessagesubjectz,$sendmessagetextz,$userid,$profileid)
{
	global $cp, $conn, $config;
    $sendmessagesubjectz=htmlentities(strip_tags($sendmessagesubjectz),ENT_QUOTES,'UTF-8');
	$sendmessagetextz=htmlentities(strip_tags($sendmessagetextz),ENT_QUOTES,'UTF-8');
	
	if ($userid != "" && is_numeric($userid) && $profileid != "" && is_numeric($profileid))
	{
		$query="INSERT INTO messages_inbox SET MSGTO='".mysql_real_escape_string($profileid)."', MSGFROM='".mysql_real_escape_string($userid)."', subject='".mysql_real_escape_string($sendmessagesubjectz)."', message='".mysql_real_escape_string($sendmessagetextz)."', time='".time()."'";
		$conn->execute($query);
		$query="insert into messages_sent set MSGTO='".mysql_real_escape_string($profileid)."', MSGFROM='".mysql_real_escape_string($userid)."', subject='".mysql_real_escape_string($sendmessagesubjectz)."', message='".mysql_real_escape_string($sendmessagetextz)."', time='".time()."'";
		$conn->execute($query);
		
		if(mysql_affected_rows()>=1)
		{			
				$query="SELECT email,username FROM members WHERE USERID='".mysql_real_escape_string($profileid)."'";
				$executequery=$conn->execute($query);
				$sendtotempemail = $executequery->fields[email];
				$sendtotemp = $executequery->fields[username];
				
				$query="SELECT email,username FROM members WHERE USERID='".mysql_real_escape_string($userid)."'";
				$executequery=$conn->execute($query);
				$tempemail = $executequery->fields[email];
				$tempusername = $executequery->fields[username];
				
				$sendername = stripslashes(stripslashes($config['site_name']));
				
				$from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='newmessage'";
            	$executequery = $conn->execute($query);
				$subject = $executequery->fields['subject']." ".$tempusername;
				
				$inboxlink = $config['baseurl']."/myinbox.php";
				
				$sendmailbody = "<html>";
				$sendmailbody .= $sendtotemp.", you just received a new message from $tempusername. To view this message, click here:<br>";
				$sendmailbody .= "<a href=$inboxlink>$inboxlink</a><br><br>";
				$sendmailbody .= "If you cannot click the link, then visit this url:<br>";
				$sendmailbody .= $inboxlink."<br><br>";
				$sendmailbody .= "$sendername";
				$sendmailbody .= "</html>";
				mailme($sendtotempemail,$sendername,$from,$subject,$sendmailbody,$bcc="");

			$sendmessagetomsg = 1;
		}
		else
		{ 
			$sendmessagetomsg = 0;
		}
	}
	else
	{
		$sendmessagetomsg = 0;
	}
		
	$a_result_node =& $cp->add_node("sendmessagetomsg");
 	$a_result_node->set_data($sendmessagetomsg);
}
// Send Message End

// Add To Friends Begin
function add_to_friends($addtofriendsmessage,$userid,$profileid)
{
	global $cp, $conn; 
    $addtofriendsmessage=htmlentities(strip_tags($addtofriendsmessage),ENT_QUOTES,'UTF-8');
	
	if ($userid != "" && is_numeric($userid) && $profileid != "" && is_numeric($profileid))
	{
		$query="SELECT RID FROM friends_requests WHERE REQUESTER='".mysql_real_escape_string($userid)."' AND REQUESTEE='".mysql_real_escape_string($profileid)."'";
		$conn->execute($query);
		
		if(mysql_affected_rows()>=1)
		{
			$addtofriendsmsg = 2;
		}
		else
		{
			$query="insert into friends_requests set REQUESTER='".mysql_real_escape_string($userid)."', REQUESTEE='".mysql_real_escape_string($profileid)."', message='".mysql_real_escape_string($addtofriendsmessage)."', time='".time()."'";
			$conn->execute($query);
	
			if(mysql_affected_rows()>=1)
			{
				$addtofriendsmsg = 1;
			}
			else
			{ 
				$addtofriendsmsg = 0;
			}
		}
	}
	else
	{
		$addtofriendsmsg = 0;
	}
		
	$a_result_node =& $cp->add_node("addtofriendsmsg");
 	$a_result_node->set_data($addtofriendsmsg);
}
// Add To Friends End

?>